package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public int save(Comment comment) {
        //1.往comment表中新增一条记录
        mongoTemplate.save(comment);

        //2.获取操作的类型
        switch (comment.getCommentType()){
            //2.1 若是点赞,修改动态表中点赞的数量+1,返回最新点赞数量
            case 1:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setLikeCount(movement.getLikeCount()+1);
                //更新
                mongoTemplate.save(movement);
                return movement.getLikeCount();
            }
            //2.2 若是评论,修改动态表中评论的数量+1,返回最新评论数量
            case 2:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setCommentCount(movement.getCommentCount()+1);
                //更新
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            //2.3 若是喜欢,修改动态表中喜欢的数量+1,返回最新喜欢数量
            case 3:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                movement.setLoveCount(movement.getLoveCount()+1);
                //更新
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }
            //2.4 2.5 2.5 空着
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public int delete(Long userId, ObjectId publishId, Integer commentType) {
        //1.从comment表中条件删除一条记录
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("publishId").is(publishId)
                .and("commentType").is(commentType)
        );
        mongoTemplate.remove(query,Comment.class);

        //2.获取操作的类型
        switch (commentType){
            //2.1 若是点赞,修改动态表中点赞的数量-1,返回最新点赞数量
            case 1:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setLikeCount(movement.getLikeCount()-1);
                //更新
                mongoTemplate.save(movement);
                return movement.getLikeCount();
            }
            //2.2 若是评论,修改动态表中评论的数量-1,返回最新评论数量
            case 2:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setCommentCount(movement.getCommentCount()-1);
                //更新
                mongoTemplate.save(movement);
                return movement.getCommentCount();
            }
            //2.3 若是喜欢,修改动态表中喜欢的数量-1,返回最新喜欢数量
            case 3:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                movement.setLoveCount(movement.getLoveCount()-1);
                //更新
                mongoTemplate.save(movement);
                return movement.getLoveCount();
            }
            //2.4 2.5 2.5 空着
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer pageNum, Integer pageSize, Integer commentType) {
        //1.创建条件对象(publishId,类型)
        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(commentType));

        //2.查询总条数
        long count = mongoTemplate.count(query, Comment.class);

        //3.追加分页和排序条件
        query.skip((pageNum-1)*pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));

        //4.查询当前页数据
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //5.封装pageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }

    @Override
    public PageBeanVo findByPage(Long publishUserId, Integer pageNum, Integer pageSize, Integer commentType) {
        //1.创建条件对象(userId和类型)
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));

        //2.查询comment表获取总条数
        long count = mongoTemplate.count(query, Comment.class);

        //3.追加条件(分页,排序)
        query.skip((pageNum-1)*pageSize).limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));

        //4.查询当前页数据集合
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //5.封装pageBeanvo返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }

}
